import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.Socket;
import java.util.Date;




public class Player extends Character{

	private boolean freeWill = true;
	private Socket socket;
	private UserOutput userOutput;
	private UserInput userInput;
	private Thread thread;
	private boolean start;
	private boolean canStart;
	String chat;
	private long charged = 0;
	private long chargeTime = 2000;  //miliseconds before attack0 fires(Game will be in half time)
	private long chargeStandardTime = 2000;  //miliseconds before attack0 fires(Game will be in half time)
	private boolean charging;
	private boolean setBlast = false;

	
	public Player(Socket socket) {
		currentLp = lpMax;
		currentEp = epMax;
		currentRp = rpMax;
		released = true;
		characterNumber = 500;
		chat = "";
		canStart = true;
		start = false;
		loaded = false;
		this.socket = socket;
		charging = false;

	}
	public boolean getStart()
	{
		return start;
	}
	public boolean getHalfTime()  //go to half time when someone is charging a ki blast
	{
		return charging;
	}
	public void start(Socket socket)
	{
		if(canStart == true)
		{
			try {
				userInput = new UserInput(this,new DataInputStream(socket.getInputStream()));
				thread = new Thread(userInput);
				thread.start();
				userOutput = new UserOutput(new DataOutputStream(socket.getOutputStream()),this);
				userOutput.start();
				start = true;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
	public void stop()
	{
		start = false;
		userInput.stop();
		userOutput.stop();
	}
	public void canStart(boolean value)
	{
		canStart = value;
	}
	public void output(String str)
	{
		if(start)
		{
			userOutput.sendData(str);
		}
	}
	public void sendChat(String str)
	{
		chat = str + "";
	}
	public String readChat()
	{
		String temp = chat + "";
		chat = "";
		return temp;
	}
	public void blastAtt()
	{
		setSprite(blastSprite);
		released = false;
		moved = false;
		currentSprite.start();
		if(setBlast == false)
		{
			Point p = new Point(0,0);
			p.setX(currentPosition.getX());
			p.setY(currentPosition.getY());
			wave = new Stream(energyBlastAttackSprite,(int)currentSprite.getSpeed(),p,getDirection(),energyDamage*2,characterNumber);
			setBlast = true;
			System.out.println("created stream");
		}
	}
	public void releaseBlast()
	{
		released = true;
		currentSprite.reset();
		currentSprite.stop();
		setBlast = false;
		wave.done();
	}
	public void engAtt0()
	{
		
		setSprite(energyBlastSprite);
		if(charged <= 2)
		{
			energyBlastSprite.stop();
			if(characterNumber == 11)
			{
				energyBlastSprite.start();
			}
			energyBlastSprite.setImg(0);
			charged = new Date().getTime();
			charging = true;
			
		}
		if(new Date().getTime() - charged  >= chargeTime)
		{
			charging = false;
			energyBlastSprite.stop();
			energyBlastSprite.lastImage();
			Point p = new Point(0,0);
			p.setX(currentPosition.getX());
			p.setY(currentPosition.getY());
			try{
				chargeTime = chargeStandardTime * 1000;
			}
			catch(Exception e)
			{
				System.out.println(e);
				chargeTime = chargeStandardTime * 10;
			}
			wave = new Wave(energyAttack0Sprite,p,getDirection(),energyDamage,characterNumber);
		}
		charged++;
			released = false;
	}
	public void releaseEngAtt0()
	{
		charging = false;
		charged = 0;
		chargeTime = chargeStandardTime;
		released = true;
		if(wave != null)
		{
			wave.done();
		}
	}
	public Socket getSocket()
	{
		return socket;
	}

	public void stopMovement(Collidable c)
	{
		wall = c;
	}
	public void updateSprite()
	{
		if(currentSprite != null)
		currentSprite.update();
	}

}
